import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class GptUtils {

    public static String getMsg(String msg) {
        String text = null;
        try {
            // URL对象表示要访问的地址
            URL url = new URL("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + msg);
            // 打开连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            conn.setRequestMethod("GET");
            // 接收响应码
            int responseCode = conn.getResponseCode();
            // 如果响应码是HTTP_OK（200），则读取响应
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                // 读取服务器响应并显示
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 打印结果
                String json = response.toString();
                String content = json.replace("content", "%s");
                text = content.substring(content.indexOf("%s") + 5, content.length() - 2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return text;
    }
}
